/**
 * Created by meiguizhinian on 2018/7/19
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIMultiLayerCheckBoxGroup = ivy.UICheckBoxGroup.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();
		this._checkBoxChildMap = new LLDictionary();
		this._findFormNamesMap = {};
		this._findFormIndexMap = new LLDictionary();
		this._blockNameFindFormName = new LLDictionary();
		this._childCheckBoxNameVec = [];
		this._formDataVec = [];
		this._checkBoxesVec = [];
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith : function(formName, blockName, sourceControlData) {
		this._super(formName,blockName,sourceControlData);
		let uim = ivy.UIManager.getInstance();
		let blockNamesStr = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_MultiLayerAniCheckBoxGroup_CheckBoxBlockNames_string]);
		let formsNamesStr = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_MultiLayerAniCheckBoxGroup_MulitFormNames_string]);
		this._newLayerTopAlway= uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_MultiLayerAniCheckBoxGroup_NewFormTop_list]);
		this._childLayerOffsetValue = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_MultiLayerAniCheckBoxGroup_ChildLayerOffset_int]);
		let separator = "|";
		this._childCheckBoxNameVec = blockNamesStr.split(separator);

		let formNamesVecTemp = formsNamesStr.split(separator);
		this._formDataVec = [];
		for (let i = 0; i < this._formDataVec.length; ++i) {
			this._formDataVec[i] = null;
		}

		let index = 0;
		for (let name of this._childCheckBoxNameVec) {
			this._childCheckBoxBlockNamesMap[name] = index;
			if (index < this._formDataVec.length) {
				this._findFormNamesMap[index]= formNamesVecTemp[index];
				this._findFormIndexMap.set(formNamesVecTemp[index], index);
				this._blockNameFindFormName.set(name, formNamesVecTemp[index]);
			}
			++index;
		}
		this.setUIElementName(formName, blockName);
	},

	setCurrentLayerByCheckBoxName : function(blockName) {
		if (this._blockNameFindFormName.has(blockName)) {
			let formName = this._blockNameFindFormName.get(blockName);
			if (this._findFormIndexMap.has(formName)) {
				let index = this._findFormIndexMap.get(formName);
				if (index >= 0 && index <= this._formDataVec.length - 1&& index !== this._currentSelectFormIndex) {
					if (this._currentSelectFormIndex >= 0) {
						if (this._formDataVec[this._currentSelectFormIndex]) {
							let newLayerTopV = this._newLayerTopAlway ? -1 : 1;
							this._formDataVec[this._currentSelectFormIndex].setLocalZOrder(this.getLocalZOrder() + this._childLayerOffsetValue+ newLayerTopV);
							this._formDataVec[this._currentSelectFormIndex].runOutAction();
						}
					}
					this._currentSelectFormIndex = index;
					for (let form of this._formDataVec.length) {
						if (form) {
							if (i === this._currentSelectFormIndex) {
								ivy.UIManager.getInstance().unRegisterSkipEnterActionElement(form.getFormName(), form.getBlockName());
							} else {
								ivy.UIManager.getInstance().registerSkipEnterActionElement(form.getFormName(), form.getBlockName());
							}
						}
					}
					let formData = this._formDataVec[index];
					if (!formData) {
						this._formDataVec[index] = ivy.UIManager.getInstance().createFormByName(formName, false);
						if (this._formDataVec[index]) {
							this.addElement(this._formDataVec[index]);
							if (cc.pointEqualToPoint(this.getSrcPosition() , this.getPosition())) {
								this._formDataVec[index].setPosition(cc.pAdd(cc.pSub(this._formDataVec[index].getPosition(), this.getSrcPosition()), this.getPosition()));
								this._formDataVec[index].setSrcPosition(this._formDataVec[index].getPosition());
							}
							this._formDataVec[index].runEnterAction();
							this._formDataVec[index].setLocalZOrder(this.getLocalZOrder() + this._childLayerOffsetValue);
						}
					} else {
						formData.setLocalZOrder(this.getLocalZOrder() + this._childLayerOffsetValue);
						formData.runEnterAction();
					}
				}
			}
		}
	},
	getCheckBoxesVec : function() {
		return this._checkBoxesVec;
	},
	getCurrentLayer : function() {
		if (this._currentSelectFormIndex >= 0 && this._currentSelectFormIndex <= this._formDataVec.length - 1) {
			return this._formDataVec[this._currentSelectFormIndex];
		}
		return null;
	},
	onExit : function() {
		for (let form of this._formDataVec) {
			if (form) {
				ivy.UIManager.getInstance().unRegisterSkipEnterActionElement(form.getFormName(),form.getBlockName());
			}
		}
		this._super();
	},
	runOutAction : function() {
		if (this._checkBoxChildMap.has(this._currentSelectFormIndex)) {
			this._checkBoxChildMap.get(this._currentSelectFormIndex).setCheckedWithoutChangeCallFunc(false);
		}
		this._super();
	},
	runEnterAction : function() {
		if (this._checkBoxChildMap.has(this._currentSelectFormIndex)) {
			this._checkBoxChildMap.get(this._currentSelectFormIndex).setCheckedWithoutChangeCallFunc(true);
		}
		this._super();
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	onChildCreateComplete : function() {
		let blockNameMap = this.getChildCheckBoxBlockNamesMap();
		for (let child of this._childrenList) {
			if (blockNameMap.has(child.getBlockName())) {
				this._checkBoxesVec.push(child);
			}
		}
		for (let checkBox of this._checkBoxesVec) {
			if (!(checkBox instanceof ivy.UICheckBox)) {
				continue;
			}
			for (let dd = 0; dd < this._childCheckBoxNameVec.length; ++dd) {
				if (checkBox.getBlockName() === this._childCheckBoxNameVec[dd]) {
					this._checkBoxChildMap.set(dd, checkBox);
					break;
				}
			}
			checkBox.setCheckBoxType(ivy.UICheckBox.UICheckBoxType.eOwnedByGroup);
			if (checkBox.isChecked()) {
				this.setCurrentLayerByCheckBoxName(checkBox.getBlockName());
			}
			checkBox.onCheckChanged().add(function(sender){
				this.setCurrentLayerByCheckBoxName(sender.getBlockName());
				let checkBoxesVec = this.getCheckBoxesVec();
				for (let checkBox of checkBoxesVec) {
					if (checkBox === sender) {
						checkBox.setCheckedWithoutChangeCallFunc(true);
					} else {
						checkBox.setCheckedWithoutChangeCallFunc(false);
					}
				}
			}.bind(this));
		}
	},
	_checkBoxChildMap : null,//std::map<int32_t, UICheckBox*> ;
	_findFormNamesMap : null,//std::map<int32_t, std::string> ;
	_findFormIndexMap : null,//std::map<std::string, int32_t> ;
	_blockNameFindFormName : null,//std::map<std::string, std::string> ;
	_childCheckBoxNameVec : null,//std::vector<std::string> ;
	_formDataVec : null,//std::vector<UIBase*> ;
	_checkBoxesVec : null,//std::vector<UIBase*> ;
	_currentSelectFormIndex : -1,
	_childLayerOffsetValue : 0,
	_newLayerTopAlway : true,
});

ivy.UIMultiLayerCheckBoxGroup.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIMultiLayerCheckBoxGroup(formName, blockName, sourceControlData);
};
